import {Component, Input, OnInit} from '@angular/core';
import {AlertController, ModalController} from '@ionic/angular';

@Component({
  selector: 'app-redpacket-list',
  templateUrl: './redpacket-list.component.html',
  styleUrls: ['./redpacket-list.component.scss']
})
export class RedpacketListComponent implements OnInit {

  @Input() title = '红包';
  @Input() list = [];
  @Input() investAmt: number;

  constructor(
    private modalCtrl: ModalController,
    private alertCtrl: AlertController
  ) {
  }

  ngOnInit() {
    this.investAmt = this.investAmt || 0;
    if (this.investAmt) {
      this.list = this.list.map(item => {
        return {...item, disabled: (item.invest_amt > this.investAmt)};
      });
    } else {
      this.list = this.list.map(item => {
        return {...item, disabled: true};
      });
    }
  }

  async checkRedpacket(item: any) {
    if (item.disabled) {
      const alert = await this.alertCtrl.create({
        header: '提示',
        message: '当前投资金额' + this.investAmt + '元，不满足使用条件',
        buttons: [
          {
            text: '知道了',
            role: 'cancel'
          }
        ]
      });
      await alert.present();
    } else {
      await this.modalCtrl.dismiss({
        grant: item
      });
    }
  }

  public closeModal() {
    this.modalCtrl.dismiss().catch(() => {
    });
  }

}
